﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WinnerCMS.Common
{
    public class MyDateTime
    {
        #region 比较日期的年份差
        /// <summary>
        /// 比较日期的年份差
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <returns>与当前日期的年份差</returns>
        public static int DiffYear(string start)
        {
            return DiffYear(Convert.ToDateTime(start));
        }

        /// <summary>
        /// 比较日期的年份差
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        /// <returns>年份差</returns>
        public static int DiffYear(string start, string end)
        {
            return DiffYear(Convert.ToDateTime(start), Convert.ToDateTime(end));
        }

        /// <summary>
        /// 比较日期的年份差
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <returns>与当前日期的年份差</returns>
        public static int DiffYear(DateTime start)
        {
            return (DiffYear(start, DateTime.Now));
        }

        /// <summary>
        /// 比较两个日期的年份差
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        /// <returns>年份差</returns>
        public static int DiffYear(DateTime start, DateTime end)
        {
            return (end.Year - start.Year);
        }
        #endregion

        #region 格式化当天日期
        /// <summary>
        /// 格式化当天日期(yyyy-MM-dd)
        /// </summary>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat()
        {
            return DateFormat(DateTime.Now);
        }

        /// <summary>
        /// 格式化日期(yyyy-MM-dd)
        /// </summary>
        /// <param name="date">待格式化的日期</param>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat(string date)
        {
            return DateFormat(Convert.ToDateTime(date));
        }

        /// <summary>
        /// 格式化日期
        /// </summary>
        /// <param name="date">待格式化的日期</param>
        /// <param name="format">格式化串</param>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat(string date, string format)
        {
            return DateFormat(Convert.ToDateTime(date), format);
        }

        /// <summary>
        /// 格式化日期(yyyy-MM-dd)
        /// </summary>
        /// <param name="date">待格式化的日期</param>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat(DateTime date)
        {
            return DateFormat(date, "yyyy-MM-dd");
        }

        /// <summary>
        /// 格式化日期
        /// </summary>
        /// <param name="date">待格式化的日期</param>
        /// <param name="format">格式化串</param>
        /// <returns>格式化后的日期字符串</returns>
        public static string DateFormat(DateTime date, string format)
        {
            return date.ToString(format);
        }

        /// <summary>
        /// 格式化日期时间
        /// </summary>
        /// <param name="datetime">待格式化的日期时间</param>
        /// <returns>格式化后的日期时间字符串</returns>
        public static string DateTimeFormat(DateTime datetime)
        {
            return DateTimeFormat(datetime, "yyyy-MM-dd HH:mm");
        }

        /// <summary>
        /// 格式化日期时间
        /// </summary>
        /// <param name="datetime">待格式化的日期时间</param>
        /// <param name="format">格式化串</param>
        /// <returns>格式化后的日期时间字符串</returns>
        public static string DateTimeFormat(DateTime datetime, string format)
        {
            return datetime.ToString(format);
        }
        #endregion

        #region 年月日差

        /// <summary>
        /// 年月日差
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static double DateDiff(DateDiffOption interval, DateTime? startDate, DateTime? endDate)
        {
            if (startDate == null)
            {
                return double.MinValue;
            }
            if (endDate == null)
            {
                return double.MaxValue;
            }
            double lngDateDiffValue = 0;
            DateTime _EndDate = (DateTime)endDate;
            DateTime _StartDate = (DateTime)startDate;
            TimeSpan ts = new TimeSpan(_EndDate.Ticks - _StartDate.Ticks);
            switch (interval)
            {
                case DateDiffOption.Milliseconds:
                    lngDateDiffValue = ts.TotalMilliseconds;
                    break;
                case DateDiffOption.Second:
                    lngDateDiffValue = ts.TotalSeconds;
                    break;
                case DateDiffOption.Minute:
                    lngDateDiffValue = ts.TotalMinutes;
                    break;
                case DateDiffOption.Hour:
                    lngDateDiffValue = ts.TotalHours;
                    break;
                case DateDiffOption.Day:
                    lngDateDiffValue = ts.Days;
                    break;
                case DateDiffOption.Week:
                    lngDateDiffValue = (ts.Days / 7);
                    break;
                case DateDiffOption.Month:
                    lngDateDiffValue = (ts.Days / 30);
                    break;
                case DateDiffOption.Quarter:
                    lngDateDiffValue = ((ts.Days / 30) / 3);
                    break;
                case DateDiffOption.Year:
                    lngDateDiffValue = (ts.Days / 365);
                    break;
            }
            return (lngDateDiffValue);
        }
       
        #endregion

        /// <summary>
        /// 输入的二个时间之差，精度：分
        /// </summary>
        /// <param name="small"></param>
        /// <returns></returns>
        public static double GetTimeSpan(DateTime small)
        {
            return GetTimeSpan(DateTime.Now, small);
        }
        /// <summary>
        /// 输入的二个时间之差，精度：分
        /// </summary>
        /// <param name="big"></param>
        /// <param name="small"></param>
        /// <returns></returns>
        public static double GetTimeSpan(DateTime big, DateTime small)
        {
            TimeSpan ts = big - small;  
            return ts.TotalMinutes;
        }

        public static int GetWeekNum(DateTime dt)
        {
            DateTime dt1 = new DateTime(dt.Year, 1, 1);
            TimeSpan ts = dt - dt1;
            return (ts.Days + (int)dt1.DayOfWeek) / 7 + 1;
        }
    }
    public enum DateDiffOption
    {
        Milliseconds, Second, Minute, Quarter, Hour, Day, Week, Month, Year

    }
}
